$("#Switchbox", window.parent.document).on("click", function () {
    ModeSwitch.modeSwitch();
    commonbgcolor();
    dynamicbgcolor();
})

$("#dynamic", window.parent.document).on("click", function () {
    getDynamic();
    iframeOn('dynamic');
})

var datetime;

//获取动态发送时间
function nowTime() {
    datetime = new Date();
}
nowTime();

//获取动态
var left = document.getElementById("leftbox");
var right = document.getElementById("rightbox");

function getDynamic() {
    left.innerHTML = '';
    right.innerHTML = '';
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '/getDynamic',
        success: function (result) {

            for (var cur = result.length - 1; 0 <= cur; cur--) {
                //判断当前动态是否有图片
                if (result[cur].length != 0) {
                    //拆分MIME字符串，并重组为字符串数组
                    var imgsrc = result[cur].imgsrc;
                    var imgArray = '';
                    var i = 0;
                    var mimeArray = result[cur].MIME.split(';');
                    mimeArray.length -= 1;

                    mimeArray.forEach(function () {
                        imgArray += `
                            <div class="imgbox">
                                <div>
                                    <img src="${imgsrc + i + '.' + mimeArray[i++]}">
                                </div>
                            </div>`
                    })
                }

                (left.offsetHeight <= right.offsetHeight) ?
                    ((result[cur].imgsrc) ?
                        (
                            left.innerHTML +=
                            `<div class="dynamic">
                                <p class="text">${result[cur].text}</p>

                                <div class="imgArray">
                                    ${imgArray}
                                </div>
                                <p class="time">${formDate(result[cur].datatime)}</p>
                            </div>`
                        ) : (
                            left.innerHTML +=
                            `<div class="dynamic">
                                <p class="text">${result[cur].text}</p>
                                <p class="time">${formDate(result[cur].datatime)}</p>
                            </div>`
                        )
                    ) :
                    ((result[cur].imgsrc) ?
                        (
                            right.innerHTML +=
                            `<div class="dynamic">
                                <p class="text">${result[cur].text}</p>

                                <div class="imgArray">
                                    ${imgArray}
                                </div>
                                <p class="time">${formDate(result[cur].datatime)}</p>
                            </div>`
                        ) : (
                            right.innerHTML +=
                            `<div class="dynamic">
                                <p class="text">${result[cur].text}</p>
                                <p class="time">${formDate(result[cur].datatime)}</p>
                            </div>`
                        )
                    )
            }
            setTimeout(() => {
                iframeOn('dynamic');
            })
        }
    })
}

//ISO时间转北京时间
function formDate(dateForm) {
    if (dateForm === "") {  //解决deteForm为空传1970-01-01 00:00:00
        return "";
    } else {
        var dateee = new Date(dateForm).toJSON();
        var date = new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
        return date;
    }
}



